package models
{
    import com.pamakids.manager.LoadManager;
    
    public class WordFilter
    {
        public function WordFilter()
        {
        }
        
        private static var _instance:WordFilter;
        
        public static function get instance():WordFilter
        {
            if(!_instance)
                _instance=new WordFilter();
            return _instance;
        }
        
        public function check(cs:String):Boolean
        {
            for each (var exp:RegExp in words) 
            {
//                trace("tets",cs,exp,exp.test(cs))
                if(exp.test(cs))
                {
//                    trace("true",cs,exp)
                    return true;
                }
            }
//			
//            return words.some(function func(item:*,index:int,array:Array) :Boolean{
//                return item.test(cs);
//            });
            return false;
        }
        
        public function pattern(cs:String):String
        {
            var rep:String;
            for each (var item:RegExp in words) 
            {
                cs=cs.replace(item,"***");
            }
            
            return cs;
        }
        
        private var words:Array
        
        public function init():void
        {
            LoadManager.instance.loadText('assets/words',loadCB);
        }
        
        private function loadCB(s:String):void
        {
            var word:Array=s.split('\n');;
            words=[];
//			var v:String
            var a:Array;
            for each (var k:String in word) 
            {
//				v=word[k];
                if(k && k.indexOf("\#")==-1)
                {
                    word.f
                    a=k.split("+");
                    a = a.map(function func(item:*,index:int,array:Array):String {
                        return "(" + (item.split('^').filter(function (str:String,...arg):Boolean{return str!=null&&str!=''}).join('|')) + ")";
                    });
                    var reg:String=a.join('');
                    words.push(new RegExp(reg));
                }
            }
        }
    }
}

